package com.lemon.secondhandplatform.util;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;

/**
 * 外部存储工具类
 */
public class ExternalStorageUtils {

    //检查SD卡是否可用
    //外围存储设备状态
    public static String STATE;
    //SD卡路径
    public static String SDCARDPATH;

    static {
        STATE = Environment.getExternalStorageState();
        if (STATE.equals(Environment.MEDIA_MOUNTED)) {
            SDCARDPATH = Environment.getExternalStorageDirectory().getPath();
        }
    }

    /**
     * 保存数据到SD卡
     * @param data
     * @param fileName
     */
    public static void save(String data, String fileName) {
        if (SDCARDPATH != null) {
            File file = new File(SDCARDPATH, fileName);
            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(data.getBytes());
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
